<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form id="test-form" action="#0" onsubmit="return false;">
        <p><label>Name: <input name="name"></label></p>
        <p><label>Email: <input name="email"></label></p>
        <p><label>Password: <input name="password" type="password"></label></p>
        <p>Gender: <label><input name="gender" type="radio" value="m" checked> Male</label> <label><input name="gender"
                    type="radio" value="f"> Female</label></p>
        <p><label>City: <select name="city">
                    <option value="BJ" selected>Beijing</option>
                    <option value="SH">Shanghai</option>
                    <option value="CD">Chengdu</option>
                    <option value="XM">Xiamen</option>
                </select></label></p>
        <p><button type="submit">Submit</button></p>
    </form>
    <!-- 输入值后，用jQuery获取表单的JSON字符串，key和value分别对应每个输入的name和相应的value，
        例如：{"name":"Michael","email":...} -->

    <script src="jquery-3.6.0.min.js"></script>
    <script>

        $('button[type= submit]').on('click', function () {
            var p1 = $("#test-form p input");
            var arr2 = [];
            
            for (let i = 0; i < p1.length-2; i++) {
                arr2.push(p1[i].value);
            }

            var p2 = $("#test-form p input[name = gender]");
            pval = -1; // 记录性别
            for (let i = 0; i < p2.length; i++) {
                if(p2[i].checked === true){
                    pval =  p2[i].value
                }
            }
            arr2.push(pval)

            var p3 = $("select"); //获取city值
            arr2.push(p3[0].value)

            var arr3 = {
                    Name:arr2[0], 
                    Email :arr2[1], 
                    Password:arr2[2] , 
                    Gender:arr2[3], 
                    City:arr2[4]
                };
            
            console.log(arr2);
            console.log(arr3);



        })

    </script>
</body>

</html>